//////////////////////////////////////////////////////////////////////////

#ifndef _MAINWINDOW_H_
#include "mainwindow.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include "ui_mainwindow.h"

#ifndef _EDITPATIENTDIALOG_H_
#include "editpatientdialog.h"
#endif

#ifndef _NEWPATIENTDIALOG_H_
#include "newpatientdialog.h"
#endif

#ifndef _FINDDIALOG_H_
#include "finddialog.h"
#endif

#ifndef _NEWRESEARCHDIALOG_H_
#include "newresearchdialog.h"
#endif

#ifndef _RESEARCHDIALOG_H_
#include "researchdialog.h"
#endif

#ifndef _ALLRESEARCHESDIALOG_H_
#include "allresearchesdialog.h"
#endif

#include <QString>
#include <QMessageBox>

//////////////////////////////////////////////////////////////////////////


MainWindow::MainWindow( QWidget* _parent )
: QMainWindow(_parent),
  ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // Create dialogs
    m_FindDialog = new FindDialog(this);
    m_EditInfoDialog = new EditPatientDialog(this);
    m_NewPatientDialog = new NewPatientDialog(this);
    m_NewResearchDialog = new NewResearchDialog(this);
    m_ResearchDialog = new ResearchDialog(this);
    m_AllResearchesDialog = new AllResearchesDialog(this);

    // Setup buttons
    connect(ui->m_FindButton, SIGNAL(clicked()), this, SLOT(FindPatient()));
    connect(ui->m_EditDetailsButton, SIGNAL(clicked()), this, SLOT(EditPatientInfo()));
    connect(ui->m_CreateNewButton, SIGNAL(clicked()), this, SLOT(NewPatient()));
    connect(ui->m_DeletePatientButton, SIGNAL(clicked()), this, SLOT(DeletePatient()));
    connect(ui->m_NewResearchButton, SIGNAL(clicked()), this, SLOT(NewResearch()));
    connect(ui->m_AllResearchesButton, SIGNAL(clicked()), this, SLOT(AllResearches()));
}


//////////////////////////////////////////////////////////////////////////


MainWindow::~MainWindow()
{
    delete ui;
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::EditPatientInfo()
{
    // Send patient to "edit info" dialog
    m_EditInfoDialog->SetPatient(m_CurrentPatient);
    
    // Show dialog
    m_EditInfoDialog->show();
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::FindPatient()
{
    // Show dialog
    m_FindDialog->show();
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::NewPatient()
{
    // Show dialog
    m_NewPatientDialog->show();
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::DeletePatient()
{
    QMessageBox box(QMessageBox::Warning, tr("Warning"),
        tr("Do you really want to delete patient?"),
        QMessageBox::Ok | QMessageBox::Cancel, this);

    box.setDefaultButton(QMessageBox::Cancel);

    switch ( box.exec() )
    {
    case QMessageBox::Ok:
        m_Database.DeletePatient(m_CurrentPatient);
        // After we deleted patient, we can not do anything
        DisablePatientGroupBox();

    default:
        break;
    }
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::NewResearch()
{
    // Show dialog
    m_NewResearchDialog->show();
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::AllResearches()
{
    // Show dialog
    m_AllResearchesDialog->show();
}


//////////////////////////////////////////////////////////////////////////


Database& MainWindow::GetDatabase()
{
    return m_Database;
}


//////////////////////////////////////////////////////////////////////////


Patient& MainWindow::GetCurrPatient()
{
    return m_CurrentPatient;
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::SetCurrPatient( const Patient& _patient )
{
    m_CurrentPatient = _patient;

    // After we have patient, we can do something with it
    EnablePatientGroupBox();
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::EnablePatientGroupBox()
{
    // Enable actions, that can be made with patient
    ui->m_EditDetailsButton->setEnabled(true);
    ui->m_DeletePatientButton->setEnabled(true);
    ui->m_NewResearchButton->setEnabled(true);
    ui->m_AllResearchesButton->setEnabled(true);

    // Display brief patient info
    ui->m_HistoryNumberView->setText( QString::number(m_CurrentPatient.GetHistoryNumber()) );
    ui->m_SurnameView->setText( m_CurrentPatient.GetSurname() );
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::DisablePatientGroupBox()
{
    // Disable actions, that can be made with patient
    ui->m_EditDetailsButton->setEnabled(false);
    ui->m_DeletePatientButton->setEnabled(false);
    ui->m_NewResearchButton->setEnabled(false);
    ui->m_AllResearchesButton->setEnabled(false);

    // Display brief patient info
    ui->m_HistoryNumberView->clear();
    ui->m_SurnameView->clear();
}


//////////////////////////////////////////////////////////////////////////


void MainWindow::ShowCurrentResearch()
{
    m_ResearchDialog->SetCurrPatient( &m_CurrentPatient );
    m_ResearchDialog->SetCurrResearch( m_CurrentPatient.GetCurrentResearch() );
    m_ResearchDialog->show();
}


//////////////////////////////////////////////////////////////////////////
